﻿///<reference path="../assets/js/jquery/jquery.js"/>
///<reference path="../assets/js/jquery/jquery.ui.all.min.js"/>
///<reference path="../assets/js/mess/IEFixer.js"/>
///<reference path="../assets/js/widgets/widgets.base.js"/>
///<reference path="../portal.js"/>

var WidgetEditorURL = KEYwebRoot + "admin/WidgetEditor.aspx";
function initdraggableZones() {
	$("div.widgetzone").each(function(i) {
		var zone = $(this);
		
		var rawWidgets = $("div.widget", zone).clone();

		//initSortableZone
		var zoneid = this.id;
		zone.sortable({
			items: "div.widget",
			opacity: 0.6,

			start: function(e, ui) {
				zone.addClass("highlight2");
			},
			stop: function(e, ui) {
				zone.removeClass("highlight2");
				fixCssClass(zone,rawWidgets);
				initAZone(zone);
			},
			update: function() {
				$.ajax({
					url: WidgetEditorURL,
					cache: false,
					data: {
						move: zone.sortable("toArray"),
						zone: zoneid
					}
				});
			}
		});
	});
}

/*---------------------------------------------------------------------------------
Helper methods
----------------------------------------------------------------------------------*/
function fixCssClass(zone,rawWidgets) 
///<summary>fix Css Class when a widget has been moved or deleted</summary>
{
	$("div.widget", zone).each(function(i) {
		var rawWidget = rawWidgets.eq(i);
		$(this).removeClass("left").removeClass("right").removeClass("endwithclearboth");
		var rawfloat = _getCssFloat(rawWidget);
		var rawclearboth = _getEndwithClearBoth(rawWidget);
		$(this).addClass(rawfloat).addClass(rawclearboth);
	});
}
function _getCssFloat(widget) {
	var rawfloat = (widget.hasClass("left") ? "left" : (widget.hasClass("right") ? "right" : ""));
	return rawfloat;
}
function _getEndwithClearBoth(widget) {
	var rawendwithclearboth = (widget.hasClass("endwithclearboth") ? "endwithclearboth" : "");
	return rawendwithclearboth;
}

/*-----------------------------------------------------------------------------
WIDGET FRAMEWORK
-----------------------------------------------------------------------------*/

function editWidget(name, id, zone) {
	window.scrollTo(0, 0);

	var size = { 'height': 610, 'width': 832 };
	var iframe = document.createElement('iframe');
	iframe.name = 'Widget Editor';
	iframe.id = 'WidgetEditor';
	iframe.src = KEYwebRoot + 'admin/widgeteditor.aspx?widget=' + name + '&id=' + id + "&zone=" + zone;
	iframe.style.height = size.height + 'px';
	iframe.style.width = size.width + 'px';

	iframe.style.backgroundColor = 'white';
	iframe.style.border = '1px solid #888';
	iframe.frameborder = '0';

	document.body.appendChild(iframe);

	$("#WidgetEditor").dialog({
		modal: true,
		width: 880,
		height: 680,
		resizable: false,
		overlay: {
			opacity: 0.5,
			background: "black"
		},
		close: function() {
			$("#WidgetEditor").dialog("destroy").remove();
			IEFixer.DialogFix("#WidgetEditor", 0);
		}
	});
	IEFixer.DialogFix("#WidgetEditor", 1);
}

function addWidget(type, zone) {
	$.ajax({
		url: WidgetEditorURL,
		cache: false,
		data: {
			add: type,
			zone: zone
		},
		success: function(res, status) {
			appendWidget(res, zone);
		}
	});
}

function appendWidget(response, zone) {
	if (response == "reload") {
		location.reload();
	}
	else {
		var zone = M$(zone);
		zone.innerHTML += response;
	}
}

function removeWidget(id, widgetPrefix, zoneid) {
	if (confirm('你确定移除此部件么?')) {
		var rawWidgets = $("div.widget",$(zoneid)).clone();
		$.ajax({
			url: WidgetEditorURL,
			cache: false,
			data: {
				remove: id,
				zone: zoneid
			},
			success: function() {
				$('#' + widgetPrefix + id).fadeOut("slow").remove();
				fixCssClass($(zoneid),rawWidgets);
				initAZone($(zoneid));
			}
		});
	}
}

function closeEditor() {
	$("#WidgetEditor").dialog("destroy").remove();
	document.body.style.position = '';
}

$(document).ready(initdraggableZones);